﻿#region zh-CHS 2006 - 2008 DemoSoft 团队 | en 2006 - 2008 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2008 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Demo.Mmose.Core.Common;
#endregion

namespace Demo.Mmose.Misc.Net.Dll
{
    /// <summary>
    /// 客户端的主处理行为
    /// </summary>
    [MultiThreadedSupport( "zh-CHS", "当前的类所有成员都可锁定,支持多线程操作" )]
    public class ConnectHandleManager
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 主处理指针
        /// </summary>
        private IntPtr m_Value      = IntPtr.Zero;
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 给出连接的服务端的处理句柄
        /// </summary>
        /// <returns></returns>
        public ConnectHandle GetClientHandler()
        {
            IntPtr newValue = IntPtr.Zero;

            if ( ConnectHandleManager.ConnectHandleManager_GetConnectHandle( ref newValue, m_Value ) == false )
            {
                Debug.WriteLine( "ConnectHandleManager.GetClientHandler(...) - ConnectHandleManager.ConnectHandleManager_GetConnectHandle(...) == false error!" );
                return null;
            }

            if ( newValue == IntPtr.Zero )
            {
                Debug.WriteLine( "ConnectHandleManager.GetClientHandler(...) - newValue == IntPtr.Zero error!" );
                return null;
            }

            ConnectHandle clientHandlerAtClient = new ConnectHandle();
            clientHandlerAtClient.Value = newValue;

            clientHandlerAtClient.SocketHandlerManager = this;

            return clientHandlerAtClient;
        }

        /// <summary>
        /// 给出新的数据区
        /// </summary>
        /// <returns></returns>
        public MessageBlock GetNewSendMessageBlock()
        {
            IntPtr newMessageBlockValue = IntPtr.Zero;
            if ( ConnectHandleManager.ConnectHandleManager_GetNewSendMessageBlock( ref newMessageBlockValue, m_Value ) == false )
            {
                Debug.WriteLine( "ConnectHandleManager.GetNewSendMessageBlock(...) - ConnectHandleManager.ConnectHandleManager_GetNewSendMessageBlock(...) == false error!" );
                return null;
            }

            if ( newMessageBlockValue == IntPtr.Zero )
            {
                Debug.WriteLine( "ConnectHandleManager.GetNewSendMessageBlock(...) - newMessageBlockValue == IntPtr.Zero error!" );
                return null;
            }

            MessageBlock newSendMessageBlock = new MessageBlock();
            newSendMessageBlock.Value        = newMessageBlockValue;

            return newSendMessageBlock;
        }
        #endregion

        #region zh-CHS 内部属性 | en Internal Properties
        /// <summary>
        /// 主处理指针
        /// </summary>
        protected internal IntPtr Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
        #endregion

        #region zh-CHS 引入DLL接口 | en DLL Import
        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtClient"></param>
        /// <param name="pSocketHandlerManagerAtClient"></param>
        /// <returns></returns>
        [DllImport("Demo.Mmose.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ConnectHandleManager_GetConnectHandle( ref IntPtr pConnectHandlerAtClient, IntPtr pSocketHandlerManagerAtClient );

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pNewSendMessageBlock"></param>
        /// <param name="pSocketHandlerManagerAtClient"></param>
        /// <returns></returns>
        [DllImport("Demo.Mmose.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ConnectHandleManager_GetNewSendMessageBlock( ref IntPtr pNewSendMessageBlock, IntPtr pSocketHandlerManagerAtClient );
        #endregion
    }
}
#endregion

